package antempire.control.game.operation.targetoperation;

import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import antempire.control.game.GameConstants;

public class TargetSprite extends AnimatedSprite implements GameConstants {

	private PhysicsHandler targetphysic, numberphysic;
	private Sprite number;
	private float speed;

	public TargetSprite(float pX, float pY, TiledTextureRegion pTextureRegion,
			TextureRegion numberTextureRegion, float speed, final BaseGameActivity activity) {
		super(pX, pY, pTextureRegion);
		number = new Sprite(pX, pY, numberTextureRegion);
		this.speed = speed;

		targetphysic = new PhysicsHandler(this);
		this.registerUpdateHandler(targetphysic);
		numberphysic = new PhysicsHandler(number);
		number.registerUpdateHandler(numberphysic);

		targetphysic.setVelocity(speed);
		numberphysic.setVelocity(speed);

		this.setUserData(number);

		activity.getEngine().getScene().getChild(LAYER_TARGET)
				.attachChild(this);
		activity.getEngine().getScene().getChild(LAYER_TARGET)
				.attachChild(number);
	}

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		if (mX < 300.f) {
			targetphysic.setVelocityX(speed);
			numberphysic.setVelocityX(speed);
		}
		if (mX > (CAMERA_WIDTH - 300)) {
			targetphysic.setVelocityX(-speed);
			numberphysic.setVelocityX(-speed);
		}
		if (mY < 150.f) {
			targetphysic.setVelocityY(speed);
			numberphysic.setVelocityY(speed);
		}
		if (mY > (CAMERA_HEIGHT - 250)) {
			targetphysic.setVelocityY(-speed);
			numberphysic.setVelocityY(-speed);
		}
		super.onManagedUpdate(pSecondsElapsed);
	}

	public float getSpeed() {
		return speed;
	}

	public void setSpeed(float speed) {
		this.speed = speed;
	}

}
